/* ***************************************************************
//  CMemcached.cpp   version:  1.0   date: 03/26/2011
//  -------------------------------------------------------------
//  Author: Wang Haoran
//  Email: cwhr_x@yahoo.com.cn
//  -------------------------------------------------------------
//  Copyright (C) 2011 HongYue - All Rights Reserved 
***************************************************************/

#include "CMemcached.h"
//#include <string>
//#include "comm_exception.h"
//using namespace C2217::StdLib;
CMemStream::CMemStream(int iType)
{
	m_strKey.clear();
	m_ss.clear();
	mp_oa = NULL;
	mp_ia = NULL;
	if(iType == CMS_TYPE_SAVE) {
		mp_oa = new boost::archive::text_oarchive(m_ss);
	} 
}

void CMemStream::SetVal(std::stringstream ssm)
{
	m_strKey.clear();
	m_ss.str(ssm.str());
	//m_ss = ssm;
	mp_oa = new boost::archive::text_oarchive(m_ss);
}

CMemStream::~CMemStream()
{
	if(mp_oa != NULL) {
		delete mp_oa;
		mp_oa = NULL;
	}

	if(mp_ia != NULL) {
		delete mp_ia;
		mp_ia = NULL;
	}

}

void CMemStream::SetKey(char *szKey)
{
	m_strKey = szKey;
}

const char* CMemStream::GetKey()
{
	return m_strKey.c_str();
}

void CMemStream::Clear()
{
	m_strKey.clear();
	m_ss.clear();
}


std::stringstream & CMemStream::GetVal()
{

	return  m_ss;
}

int CMemStream::GetValLen()
{
	return (int)m_ss.str().size();
}

void CMemStream::SetVal(char * p)
{	
	m_ss.str(p);
	
	if(mp_ia != NULL) {
		delete mp_ia;
		mp_ia = NULL;
	}
	mp_ia = new boost::archive::text_iarchive(m_ss);

	return ;
}

void CMemcached::SetKeyVal(CMemStream& cmstream)
{
	char * p = (char*)cmstream.GetVal().str().c_str();
	int len = cmstream.GetValLen();
	SetKeyVal((char*)cmstream.GetKey(), (char*)cmstream.GetVal().str().c_str(), cmstream.GetValLen());

}

void CMemcached::GetKeyVal(CMemStream& cmstream){

	return cmstream.SetVal(GetKeyVal((char*)cmstream.GetKey()));
}

CMemcached::CMemcached(char* szSvrName, unsigned int iSvrPost, memcached_behavior be)
{
	m_memc = NULL;
	m_memc = memcached_create(NULL);
	if(m_memc == NULL) {
		THROW_EX("memcached_create failed");
	}
	m_rc = memcached_server_add(m_memc, szSvrName, iSvrPost);
	if(m_rc != MEMCACHED_SUCCESS) {
		//THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	this->SetBehavior(be, 1);
}

CMemcached::~CMemcached()
{
	if(m_memc != NULL)
	memcached_free(m_memc);
}
memcached_return CMemcached::FlushAll()
{
	m_rc = memcached_flush(m_memc, 0);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	return m_rc;
}
memcached_return CMemcached::SetBehavior(memcached_behavior be, uint64_t val)
{
	m_rc = memcached_behavior_set(m_memc, be, val);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}

	return m_rc;
}

uint64_t CMemcached::IncrementKeyVal(char* KEY)
{
	uint64_t num;
	m_rc = memcached_increment(m_memc, KEY, strlen(KEY), 1, &num);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	return num;
}

void CMemcached::DeleteKey(char* KEY)
{
	m_rc = memcached_delete(m_memc, KEY, strlen(KEY), 0);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	return ;
}


void CMemcached::SetKeyVal(char* KEY, char * val)
{
	m_rc = memcached_set(m_memc, KEY, strlen(KEY), val, strlen(val), 0, 0);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	return ;
}


void CMemcached::SetKeyVal(char* KEY, char * val, int iLen)
{
	m_rc = memcached_set(m_memc, KEY, strlen(KEY), val, iLen, 0, 0);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}
	return ;
}
char* CMemcached::GetErrString()
{

	
	return memcached_strerror(m_memc, m_rc);

}

bool CMemcached::IsKeyExist(char *KEY)
{
	char * pszVal = NULL;
	size_t value_length = 0;
	uint32_t flags = 0;	
	bool bFound = false;
	pszVal = memcached_get(m_memc, KEY, strlen(KEY), &value_length, &flags, &m_rc);
	if(m_rc == MEMCACHED_SUCCESS) {
		bFound = true;
	} else if(m_rc == MEMCACHED_NOTFOUND) {
		bFound = false;
	} else {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}

	return bFound;
}

char * CMemcached::GetKeyVal(char *KEY)
{
	char * pszVal = NULL;
	size_t value_length = 0;
	uint32_t flags = 0;

	pszVal = memcached_get(m_memc, KEY, strlen(KEY), &value_length, &flags, &m_rc);
	if(m_rc != MEMCACHED_SUCCESS) {
		THROW_MY_CODE0(m_rc, memcached_strerror(m_memc, m_rc));
	}

	return pszVal;
}